using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
using System.Text;

namespace CodeGenTest
{
    class Program
    {
        

        static void Main(string[] args)
        {
            Expr val1 = new Value(23);
            Expr val2 = new Value(33);
            Expr val3 = new RandomValue();

            Expr add1 = new AddOp(val1, val2);
            Expr mul1 = new MultiPly(add1, val3);
            Expr add2 = new AddOp(add1, mul1);

            Console.WriteLine("The final expr is: " + add2);

            Console.ReadKey();

            CodeGen codeG = new CodeGen(add2);
            StandardMethod method = codeG.GetMethod();

            Console.WriteLine("Method result: " + method());

            Console.ReadKey();
            
        }

    }

    public class TestClass
    {
        private delegate bool Method();
        private string Peter = "Jamen, hephey da";

        static void Test()
        {
            DynamicMethod method = new DynamicMethod("Test", typeof(int), new Type[0], typeof(TestClass));

            ILGenerator ilg = method.GetILGenerator();

            LocalBuilder varInt = ilg.DeclareLocal(typeof(int));
            

            //ilg.Emit(OpCodes.Ldc_I4, 33);
            //ilg.Emit(OpCodes.Stloc, varInt);

            ilg.Emit(OpCodes.Ldloc, varInt);
            ilg.Emit(OpCodes.Isinst, typeof(int));
            //ilg.Emit(OpCodes.Brtrue, );

            //ilg.Emit(OpCodes.Ldc_I4_2);
            //ilg.Emit(OpCodes.Add);
            ilg.Emit(OpCodes.Ret);

            Method mth = (Method)method.CreateDelegate(typeof(Method));


            Console.WriteLine("Method run: " + mth());
            Console.ReadLine();
        }
    }
}
